package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Disease;
import com.foreknow.service.DiseaseService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "DiseaseController", description = "疾病查询")
@RestController
@RequestMapping("/diseaseController")
public class DiseaseController {
    @Autowired
    private DiseaseService diseaseService;
    @ApiOperation("通过Id查询")
    @RequestMapping(value = "/selectDiseaseList", method = RequestMethod.POST)
    @ResponseBody

    public String selectDiseaseList(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Integer disecategoryid = (Integer) para.get("disecategoryid");
        Integer pageNum = (Integer) para.get("pageNum");
        Integer pageSize = (Integer) para.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        //调用Service 的方法
        List<Disease> list = diseaseService.selectDiseaseList(disecategoryid);
        PageInfo<Disease> personPageInfo = new PageInfo<>(list);
        HashMap<String, Object> hs = new HashMap<>();
        if (list != null) {
            hs.put("data", list);
            hs.put("personPageInfo", personPageInfo);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    //添加
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    @ResponseBody
    public String insertSelective(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Disease disease = (Disease) para.get("disease");
        //调用Service 的方法
        int aa = diseaseService.insertSelective(disease);
        HashMap<String, Object> hs = new HashMap<>();
        if (aa != 0) {
            hs.put("data", aa);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/selectDiseaseByDisease", method = RequestMethod.POST)
    @ResponseBody
//    模糊查询
    public String selectDiseaseByDisease(@RequestBody Disease disease) throws JsonProcessingException {
        disease.setDiseasecode(disease.getDiseasename());
        Integer pageNum = disease.getPageNum();
        Integer pageSize = disease.getPageSize();
        //调用Service 的方法
        System.out.println(disease);
        System.out.println("----------------------------");
        System.out.println(disease);
        System.out.println("----------------------------");
        PageHelper.startPage(pageNum, pageSize);
        List<Disease> list = diseaseService.selectDiseaseByDisease(disease);
        PageInfo<Disease> personPageInfo = new PageInfo<>(list);
//        System.out.println(diseCategory3);
//        System.out.println(diseCategory3.size());
        HashMap<String, Object> hs = new HashMap<>();
        if (list != null) {
            hs.put("data", list);
            hs.put("personPageInfo", personPageInfo);
            hs.put("meta", new Constance("msg", 200));

        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
//    修改前进行查询在表单显示


    @ApiOperation("通过diseasename查询")
    @RequestMapping(value = "/selectDisease", method = RequestMethod.POST)
    @ResponseBody
    public String selectDisease(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        String disename1 = (String) para.get("diseasename");
        //调用Service 的方法,返回一个对象
        Disease disease = diseaseService.selectDisease(disename1);
        HashMap<String, Object> hs = new HashMap<>();
        if (disease != null) {
            hs.put("data", disease);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    /**
     * 对疾病信息进行修改
     *
     * @param disease
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/updateByPrimaryKeySelective", method = RequestMethod.POST)
    @ResponseBody
    public String updateByPrimaryKeySelective(@RequestBody Disease disease) throws JsonProcessingException {
        Disease disease1 = disease;
        //调用Service 的方法
        int num = diseaseService.updateByPrimaryKeySelective(disease);
        HashMap<String, Object> hs = new HashMap<>();
        if (num != 0) {
            hs.put("data", num);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
//    }
    }

    //        diseaseController
    @ApiOperation("根据 id进行逻辑删除")
    @RequestMapping(value = "/deleteDisease", method = RequestMethod.PUT)
    @ResponseBody
    public String deleteDisease(@RequestBody Disease disease) throws JsonProcessingException {
// //调用Service 的方法
        int id = disease.getId();
        int num = diseaseService.deleteDisease(id);
        HashMap<String, Object> hs = new HashMap<>();
        if (num != 0) {
            hs.put("data", num);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


}
